package yxy.game.pm2.module.guard;

import java.util.HashSet;
import java.util.Set;

import org.redisson.api.RSet;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.BaseGameRedisBean;

public class GuardSubscriber extends BaseGameRedisBean {
	private int areaId;

	public GuardSubscriber(int areaId) {
		this.areaId = areaId;
	}

	public int getAreaId() {
		return areaId;
	}

	// redis==========================================================================

	private static String key(int areaId) {
		return String.format("AREA:%s:MODULE:GUARDSUB", areaId);
	}

	static private RSet<String> touch(String key) throws RedisException {
		return RedisManager.client(redis()).getSet(key);
	}

	private RSet<String> touch() throws Exception {
		return touch(key(getAreaId()));
	}

	public void add(String playerCode) {
		try {
			touch().add(playerCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Set<String> clear() {
		try {
			Set<String> set = touch().readAll();
			touch().clear();
			return set;
		} catch (Exception e) {
			return new HashSet<>();
		}
	}

	static public GuardSubscriber get(String playerCode) {
		AreaBean areaModule = AreaModule.area(playerCode);
		return get(areaModule.getAreaId());
	}

	static public GuardSubscriber get(int areaId) {
		return new GuardSubscriber(areaId);
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return null;
	}
}
